using System;
using System.Threading.Tasks;
using JlPay.Sdk.Core;
using JlPay.Sdk.Models.Ledger.Bill;

namespace JlPay.Sdk.Services.Ledger.Bill
{
    /// <summary>
    /// 账单服务类
    /// </summary>
    public class BillService : BaseService
    {
        /// <summary>
        /// 创建账单服务
        /// </summary>
        /// <param name="config">配置对象</param>
        public BillService(Config config) : base(config)
        {
        }

        /// <summary>
        /// 获取API URL
        /// </summary>
        /// <param name="requestType">请求类型</param>
        /// <returns>API URL</returns>
        private string GetApiUrl(Type requestType)
        {
            var apiPrefix = requestType.GetField("ApiPrefix")?.GetValue(null)?.ToString();
            var apiUri = requestType.GetField("ApiUri")?.GetValue(null)?.ToString();
            
            if (!string.IsNullOrEmpty(apiPrefix) && !string.IsNullOrEmpty(apiUri))
            {
                return "/" + apiPrefix.TrimStart('/') + apiUri;
            }
            
            return "/open/ledger/bill";
        }

        /// <summary>
        /// 申请账单下载URL
        /// </summary>
        /// <param name="request">请求对象</param>
        /// <returns>响应对象</returns>
        public async Task<BillApplyUrlResponse> ApplyBillUrlAsync(BillApplyUrlRequest request)
        {
            var apiPath = GetApiUrl(typeof(BillApplyUrlRequest));
            return await PostAsync<BillApplyUrlRequest, BillApplyUrlResponse>(request, apiPath);
        }
    }
} 